-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.cpp
More file actions
36 lines (31 loc) · 787 Bytes
/
Solution.cpp
File metadata and controls
36 lines (31 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>
using namespace std;
int removeDuplicates(vector<int>& arr) {
if (arr.empty()) return 0;
int j = 0; // Index for the next unique element
for (int i = 1; i < arr.size(); i++) {
if (arr[i] != arr[j]) {
j++;
arr[j] = arr[i];
}
}
return j + 1;
}
int main() {
int n;
cout << "Enter the size of the sorted array: ";
cin >> n;
vector<int> arr(n);
cout << "Enter " << n << " elements of the sorted array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
int newSize = removeDuplicates(arr);
cout << "Array after removing duplicates: ";
for (int i = 0; i < newSize; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}